package com.gnerv.author.entity;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Gnerv Li Gen
 * @description 返回数据通用统一类
 * @since 2019-05-14
 */
public class R extends HashMap<String, Object> {

    private static final long serialVersionUID = 1L;

    public static Object[] CODE_200 = {200, "OK"};

    public static Object[] CODE_400 = {400, "请求参数错误！"};
    public static Object[] CODE_401 = {401, "用户未登录或者登录失效！"};
    public static Object[] CODE_405 = {405, "权限不足！"};

    public static Object[] CODE_500 = {500, "服务器内部错误，联系管理员！"};

    private Map<String, Object> data = new HashMap<>();

    public R() {
        put("code", 200);
        put("msg", "ok");
        put("data", data);
    }

    public R(Object[] strings) {
        put("code", strings[0]);
        put("msg", strings[1]);
        put("data", data);
    }

    public R(Object[] strings, String msg) {
        put("code", strings[0]);
        put("msg", msg);
        put("data", data);
    }

    public R(Integer code, String msg) {
        put("code", code);
        put("msg", msg);
        put("data", data);
    }

    public static R back() {
        return new R();
    }

    public static R back(Object[] strings) {
        return new R(strings);
    }

    public static R back(Object[] strings, String msg) {
        return new R(strings, msg);
    }

    public static R back(Integer code, String msg) {
        return new R(code, msg);
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public R data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public R data(Map<String, Object> map) {
        this.data.putAll(map);
        return this;
    }

}
